package com.ruoyi.controller;

import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.domain.MonthUserStatisticsDTO;
import com.ruoyi.domain.SaveRescue;
import com.ruoyi.domain.ServiceTypeCount;
import com.ruoyi.service.CarCorderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/CarCorder")
public class CarCorderController {
    @Autowired
    private CarCorderService carCorderService;
    /**
     * 获取指定日期的订单总金额
     */
    @RequestMapping ("/summoney")
    public AjaxResult sumOrderAmountByDate(
            @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") Date date) {

        if (date == null) {
            return AjaxResult.error("请提供有效的日期参数");
        }
        return AjaxResult.success(carCorderService.sumOrderAmountByDate(date));
    }

    /**
     * 获取指定日期的订单总数
     */
    @RequestMapping ("/countorder")
    public AjaxResult countOrdersByDate(
            @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") Date date) {

        if (date == null) {
            return AjaxResult.error("请提供有效的日期参数");
        }

        int count = carCorderService.countOrdersByDate(date);
        return AjaxResult.success(count);
    }

    @RequestMapping ("/useraddcount")
    public AjaxResult userAddCount(
            @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") Date date) {

        if (date == null) {
            return AjaxResult.error("请提供有效的日期参数");
        }

        int count = carCorderService.userAddCount(date);
        return AjaxResult.success(count);
    }

    @RequestMapping ("/merchantaddcount")
    public AjaxResult merchantAddCount(
            @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") Date date) {

        if (date == null) {
            return AjaxResult.error("请提供有效的日期参数");
        }

        int count = carCorderService.merchantAddCount(date);
        return AjaxResult.success(count);
    }

    /**
     * 查询商户总数
     */
    @RequestMapping ("/merchantcount")
    public AjaxResult merchantCount() {
        return AjaxResult.success(carCorderService.merchantCount());
    }

    /**
     * 获取用户总数
     */
    @RequestMapping ("/usercount")
    public AjaxResult userCount() {
        return AjaxResult.success(carCorderService.userCount());
    }

    /**
     * 查询指定日期各服务类别的订单数量
     * @param date 日期（格式：yyyy-MM-dd）
     * @return 统计结果
     */
    @RequestMapping ("/typecount")
    public AjaxResult countOrderByServiceType(
            @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") Date date) {

        if (date == null) {
            return AjaxResult.error("请提供有效的日期参数");
        }

        ServiceTypeCount result = carCorderService.countOrderByServiceType(date);
        return AjaxResult.success(result);
    }

    /**
     * 查询指定日期各服务类别的订单数量
     * @return 统计结果
     */
    @RequestMapping ("/alltypecount")
    public AjaxResult countAllOrderByServiceType() {
        ServiceTypeCount result = carCorderService.countAllOrderByServiceType();
        return AjaxResult.success(result);
    }

    /**
     * 根据日期查询当天救援任务处理情况
     * @param date 目标日期
     */
    @RequestMapping ("/countsave")
    public AjaxResult countSaveRescue(
            @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") Date date) {

        if (date == null) {
            return AjaxResult.error("请提供有效的日期参数");
        }

        SaveRescue result = carCorderService.countSaveRescue(date);
        return AjaxResult.success(result);
    }

    /**
     * 查询全部救援任务处理情况
     */
    @RequestMapping ("/countallsave")
    public AjaxResult countAllSaveRescue() {
        SaveRescue result = carCorderService.countAllSaveRescue();
        return AjaxResult.success(result);
    }

    /**
     * 获取指定月份的新增用户统计数据
     * 保留年月日参数，但以月份为统计单位
     */
    @RequestMapping ("tongjitu")
    public Map<String, Object> getMonthlyUserStats(
            @RequestParam int year,
            @RequestParam int month,
            @RequestParam(required = false) Integer day) {  // 日期参数保留但不使用

        try {
            MonthUserStatisticsDTO stats = carCorderService.getMonthlyUserRegistrationStats(year, month);

            Map<String, Object> result = new HashMap<>();
            result.put("code", 200);
            result.put("msg", "查询成功");
            result.put("data", stats);
            return result;
        } catch (Exception e) {
            Map<String, Object> error = new HashMap<>();
            error.put("code", 500);
            error.put("msg", "查询失败：" + e.getMessage());
            return error;
        }
    }
}
